package treeHoleUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.*;

import treeHoleDAO.AccountInformationIMPH;
import treeHoleDAO.PublicTreeHoleIMPH;
import treeHoleSERVICE.*;

/**
 * @program:IntelliJ IDEA
 * @Description: 树洞界面
 * @author:Mr.CheCheHuiBujianO
 * @data: 2022/6/16 10:30
 */
public class TreeHoleInterface extends JFrame {
    public String getUserName() {
        return this.userName;
    }

    public  void setUserName(String userName) {
        this.userName = userName;
    }

    //需要一个 获取所有人文章的方法(获取树洞界面所需要的文章)
    //查所有的树洞文章-> 根据每个文章里面的用户id  找对应的用户名 -> 打印用户名和文章
    private static String userName = null;

    private int otherUserAccount;
    /**
     * Constructs a new frame that is initially invisible.
     * <p>
     * This constructor sets the component's locale property to the value
     * returned by <code>JComponent.getDefaultLocale</code>.
     *
     * @throws HeadlessException if GraphicsEnvironment.isHeadless()
     *                           returns true.
     * @see GraphicsEnvironment#isHeadless
     * @see Component#setSize
     * @see Component#setVisible
     * @see JComponent#getDefaultLocale
     */
    public TreeHoleInterface() throws HeadlessException {
        initComponent();
    }

    List<User.UserHistory> userHistories = new PublicTreeHoleIMPH().readTreeHoleFileAndShowUI();

    private void userUI(ActionEvent e){
        this.dispose();//当前窗口销毁
        new PersonalInformationInterface().setVisible(true);//显示用户界面
    }


    //是否退出树洞界面
    private void exitSystem(ActionEvent e){
        int choose = JOptionPane.showConfirmDialog(this, "是否退出树洞", "退出界面", JOptionPane.YES_OPTION);
        ExitSystem exitSystem = new ExitSystemIMPH();
        exitSystem.whetherExitSystem(choose);
    }

    //发帖对话框
    private void PostMessage(ActionEvent event){
        JDialog dialog = new JDialog(this,"发帖",true);


        JLabel label1 = new JLabel();
        JLabel label2 = new JLabel();
        JTextField textField1 = new JTextField(20);
        JScrollPane scrollPane1 = new JScrollPane();
        JTextArea textArea1 = new JTextArea();
        JButton button1 = new JButton();
        JButton button2 = new JButton();
        JPanel  jPanel = new JPanel();


        jPanel.setLayout(null);
        //---- label1 ----
        label1.setText("标题");
        jPanel.add(label1);
        label1.setBounds(5, 30, 40, 20);

        //---- label2 ----
        label2.setText("内容");
        jPanel.add(label2);
        label2.setBounds(5, 60, 40, 20);

        textArea1.setEditable(true);//可编辑
        jPanel.add(textField1);
        textField1.setBounds(45,25, 345, 25);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(textArea1);
            textArea1.setLineWrap(true);
            textArea1.setEditable(true);
        }
        jPanel.add(scrollPane1);
        scrollPane1.setBounds(45, 55, 345, 150);

        button1.setText("发布");
        jPanel.add(button1);
        button1.addActionListener(e->{
            int choose = JOptionPane.showConfirmDialog(dialog, "是否发布", "发布界面", JOptionPane.YES_OPTION);
            if (choose == 0){
                //标题内容
                String titleContent = textField1.getText();
                //文章内容
                String essayContent = textArea1.getText();
                //获取当前时间
                Date releaseTime =  new Date();
                //获取当前用户账号信息
                User user = new User();
                User.UserHistory userHistory = user.new UserHistory();
                user.userEssayCollection = new ArrayList<>();
                userHistory.title = titleContent;
                userHistory.releaseTime = releaseTime;
                userHistory.essayContent = essayContent;
                userHistory.giveLikeAccount = 0;
                //还需要去确定当前账号
                userHistory.userAccount = Integer.parseInt(this.button1.getText());
//                if(user.userEssayCollection == null)
                //这个用户账号是从登陆页面得到的，如果登陆成功就将输入的账号返回出来
                // user.userAccount = new TreeHoleShowIMPH().userAccount(true);
                user.userEssayCollection.add(userHistory);
                user.userPassWord = null;//因为这是发布页面所以将用户密码设置为空
                PostMessage postMessage = new PostMessageIMPH();
                postMessage.postMessage(user);
//                dispose();
            }
        });
        button1.setBounds(180, 205, 85, 40);

        button2.setText("退出");
        jPanel.add(button2);
        button2.addActionListener(e->{
            int choose = JOptionPane.showConfirmDialog(dialog, "是否退出发帖", "退出界面", JOptionPane.YES_OPTION);
            if (choose == 0)dialog.dispose();
        });
        button2.setBounds(285, 205, 80, 40);

        dialog.setContentPane(jPanel);
        dialog.setSize(460,300);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }


    private void initComponent(){


        panel1 = new JPanel();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button6 = new JButton();
        scrollPane1 = new JScrollPane();
        panel2 = new JPanel();
        int TextNumber = 1;
        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());



        //======== LeftSelf ========
        {
            panel1.setLayout(new GridBagLayout());
            ((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {0, 0};
            ((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0};
            ((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};
            ((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};


            //---- button1 ----
            Integer userAccount = new LoginInterface().getUserAccount1();
            button1.setText(userAccount.toString());
            button1.addActionListener(e->userUI(e));
            panel1.add(button1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 0), 0, 0));

            //---- button2 ----
            button2.setText("发贴");
            button2.addActionListener(e -> PostMessage(e));
            panel1.add(button2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 0), 0, 0));
            //---- button3 ----
            button3.setText("退出");
            button3.addActionListener(e -> exitSystem(e));
            panel1.add(button3, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 0), 0, 0));

            button6.setText("个人页面");
            button6.addActionListener(e->userUI(e));
            panel1.add(button6, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
        }
        contentPane.add(panel1,BorderLayout.WEST);

        for (User.UserHistory UserHistory : userHistories){

            JPanel TextSelf1 = new JPanel();
            JScrollPane scrollPane2 = new JScrollPane();
            JTextArea TitleSelf1 = new JTextArea();
            JScrollPane scrollPane3 = new JScrollPane();
            JTextArea ArticleSelf1 = new JTextArea();
            JButton Delete1 = new JButton();
            JButton LikeSelf1 = new JButton();

            //======== scrollPane1 ========
            {

                //======== CenterSelf ========
                {
                    panel2.setLayout(new GridBagLayout());

                    //======== TextSelf1 ========
                    {
                        TextSelf1.setLayout(new GridBagLayout());

                        //======== scrollPane2 ========
                        {
                            //======== TitleSelf1 ========
                            {
//                                String Title1 = UserHistory1.readTitle();
                                //调用读取List中标题的方法，返回一个Title1
//                                TitleSelf1.append(Article1);
                                TitleSelf1.setLineWrap(true);
                                TitleSelf1.append(UserHistory.title);
                                TitleSelf1.setEditable(false);
                            }
                            scrollPane2.setViewportView(TitleSelf1);
                        }
                        TextSelf1.add(scrollPane2, new GridBagConstraints(0, 1, 8, 1, 0.0, 0.0,
                                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                                new Insets(0, 0, 5, 0), 0, 0));

                        //======== scrollPane3 ========
                        {
                            //======== ArticleSelf1 ========
                            {
//                                String Article1 = UserHistory1.readArticle();
                                //调用读取List中文章的方法，返回一个Article1
//                                ArticleOther1.append(Article1);
                                ArticleSelf1.setLineWrap(true);
                                ArticleSelf1.append(UserHistory.essayContent);
                                ArticleSelf1.setEditable(false);
                            }
                            scrollPane3.setViewportView(ArticleSelf1);
                        }
                        TextSelf1.add(scrollPane3, new GridBagConstraints(0, 2, 8, 6, 0.0, 0.0,
                                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                                new Insets(0, 0, 5, 0), 0, 0));

                        //---- Delete1 ----
                        Delete1.setText("点赞");
                        TextSelf1.add(Delete1, new GridBagConstraints(6, 8, 1, 1, 0.0, 0.0,
                                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                                new Insets(0, 0, 0, 5), 0, 0));

                        int userAccount = UserHistory.userAccount;
                        String userName = new AccountInformationIMPH().userName(userAccount);
                        LikeSelf1.setText(userName);
                        LikeSelf1.addActionListener(e -> {
                            setUserName(LikeSelf1.getText());
                            this.dispose();
                            new OtherInformationInterface().setVisible(true);
                        });
                        TextSelf1.add(LikeSelf1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                                new Insets(0, 0, 0, 0), 0, 0));
                    }
                    panel2.add(TextSelf1, new GridBagConstraints(0, TextNumber, 1, 1, 0.0, 0.0,
                            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                            new Insets(0, 0, 5, 0), 0, 0));
                }
                scrollPane1.setViewportView(panel2);
                TextNumber++;
            }
        }
        contentPane.add(scrollPane1, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }



    private JPanel panel1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button6;
    private JScrollPane scrollPane1;
    private JPanel panel2;
}
